package com.bo.controller;

import com.bo.anno.Log;
import com.bo.pojo.Emp;
import com.bo.pojo.PageBean;
import com.bo.pojo.Result;
import com.bo.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * @author huangbo
 * @version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    //分页查询
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("分页查询，参数为:{},{},{},{},{},{}", page, pageSize, name, gender, begin, end);
        PageBean page1 = empService.page(page, pageSize, name, gender, begin, end);
        return Result.success(page1);
    }

    //批量删除
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids){
        log.info("批量删除id="+ids+"的员工");
        empService.delete(ids);
        return Result.success();
    }

    //添加员工
    @Log
    @PostMapping
    public Result insert(@RequestBody Emp emp){
        log.info("添加员工，信息为："+emp);
        empService.insert(emp);
        return Result.success();
    }

    //查询回显
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        log.info("查询员工，id={}",id);
        Emp emp = empService.get(id);
        return Result.success(emp);
    }

    //修改员工
    @Log
    @PutMapping
    public Result change(@RequestBody Emp emp){
        log.info("修改员工，id={}",emp.getId());
        empService.change(emp);
        return Result.success();
    }

}
